package beyjava.model;

import java.util.Date;
import java.util.Vector;


/**
 * @author BEYJAVA
 *
 */
public class Task {
	private  Integer taskNumber;
	private  String taskDesc;
	private Date taskStartDate;
	private Integer taskHours;
	private Vector<Person> taskResource;
	private Activity actOfTask;
	/**
	 * @param taskNumber
	 * @param taskDesc
	 * @param taskStartDate
	 * @param taskHours
	 * @param actOfTask
	 * constructor of task class
	 */
	public Task(Integer taskNumber, String taskDesc, Date taskStartDate, Integer taskHours,
			Activity actOfTask ) {
		super();
		this.taskNumber = taskNumber;
		this.taskDesc = taskDesc;
		this.taskStartDate = taskStartDate;
		this.taskHours = taskHours;
		this.taskResource = new Vector<Person>();
		this.actOfTask = actOfTask;
	}
	/**
	 * @param taskNumber
	 * @param taskDesc
	 * @param taskStartDate
	 * @param taskHours
	 * @param actOfTask
	 * @return task to check it is null or not
	 */
	public static Task createTask(Integer taskNumber, String taskDesc, Date taskStartDate, Integer taskHours,
			Activity actOfTask){
		Task task = null;
		if(actOfTask != null){
			task = new Task(taskNumber , taskDesc ,taskStartDate ,taskHours ,actOfTask);
			actOfTask.addTaskToActivity(task);
		}
		return task;
	}
	/**
	 * @param resource
	 */
	public void assignResourceToTask(Resource resource){
		taskResource.add((Person) resource);
	}
	/**
	 * @param rs
	 */
	public void unassignResourceFromTask(Resource rs){
		taskResource.remove(rs);
	}
	/**
	 * @param taskNumber
	 */
	public void setTaskNumber(Integer taskNumber) {
		this.taskNumber = taskNumber;
	}
	/**
	 * @return taskNumber
	 */
	public Integer getTaskNumber() {
		return taskNumber;
	}
	/**
	 * @param taskDesc
	 */
	public void setTaskDesc(String taskDesc) {
		this.taskDesc = taskDesc;
	}
	/**
	 * @return taskDesc
	 */
	public String getTaskDesc() {
		return taskDesc;
	}
	/**
	 * @param taskStartDate
	 */
	public void setTaskStartDate(Date taskStartDate) {
		this.taskStartDate = taskStartDate;
	}
	/**
	 * @return taskStartDate
	 */
	public Date getTaskStartDate() {
		return taskStartDate;
	}
	/**
	 * @param taskHours
	 */
	public void setTaskHours(Integer taskHours) {
		this.taskHours = taskHours;
	}
	/**
	 * @return taskHours
	 */
	public Integer getTaskHours() {
		return taskHours;
	}	
	/**
	 * @param actOfTask
	 */
	public void setActOfTask(Activity actOfTask) {
		this.actOfTask = actOfTask;
	}
	/**
	 * @return actOfTask
	 */
	public Activity getActOfTask() {
		return actOfTask;
	}
	/**
	 * @param newTask
	 * @return true if the task is updated correctly
	 */
	public boolean updateTask(Task newTask){
		 if(newTask !=null){
			 if(newTask.getTaskHours().intValue()!= this.getTaskHours().intValue()){
				 this.actOfTask.setHours(this.actOfTask.getHours()+(newTask.getTaskHours()-this.getTaskHours()));
			 }
			 this.setTaskDesc(newTask.getTaskDesc());
			 this.setTaskHours(newTask.getTaskHours());
			 this.setTaskResource(newTask.getTaskResource());
			 this.setTaskStartDate(newTask.getTaskStartDate());
			 return true;
		 }
		 return false;
	}
	/**
	 * @param newResources
	 */
	public void setTaskResource(Vector<Person> newResources) {
		this.taskResource = newResources;
	}
	/**
	 * @return taskResource
	 */
	public Vector<Person> getTaskResource() {
		return taskResource;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((actOfTask == null) ? 0 : actOfTask.hashCode());
		result = prime * result
				+ ((taskDesc == null) ? 0 : taskDesc.hashCode());
		result = prime * result
				+ ((taskHours == null) ? 0 : taskHours.hashCode());
		result = prime * result
				+ ((taskNumber == null) ? 0 : taskNumber.hashCode());
		result = prime * result
				+ ((taskResource == null) ? 0 : taskResource.hashCode());
		result = prime * result
				+ ((taskStartDate == null) ? 0 : taskStartDate.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Task other = (Task) obj;
		if (actOfTask == null) {
			if (other.actOfTask != null)
				return false;
		} else if (!actOfTask.equals(other.actOfTask))
			return false;
		if (taskDesc == null) {
			if (other.taskDesc != null)
				return false;
		} else if (!taskDesc.equals(other.taskDesc))
			return false;
		if (taskHours == null) {
			if (other.taskHours != null)
				return false;
		} else if (!taskHours.equals(other.taskHours))
			return false;
		if (taskNumber == null) {
			if (other.taskNumber != null)
				return false;
		} else if (!taskNumber.equals(other.taskNumber))
			return false;
		if (taskResource == null) {
			if (other.taskResource != null)
				return false;
		} else if (!taskResource.equals(other.taskResource))
			return false;
		if (taskStartDate == null) {
			if (other.taskStartDate != null)
				return false;
		} else if (!taskStartDate.equals(other.taskStartDate))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "Task [taskNumber=" + taskNumber + ", taskDesc=" + taskDesc
				+ ", taskStartDate=" + taskStartDate + ", taskHours=" + taskHours
				+ ", taskResource=" + taskResource + ", actOfTask=" + actOfTask
				+ "]";
	}

}
